package com.controller.sys;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.Result;
import com.config.security.JwtTokenUtil;
import com.entity.sys.Message;
import com.service.sys.MessageService;
import com.service.sys.UserVoService;
import com.vo.sys.MessageVo;
import com.vo.sys.UserExceptSelfVo;
import com.vo.sys.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api("MessageController")
@RestController
@RequestMapping("/sys/msg")
public class MessageController {

    @Resource
    private MessageService messageService;

    @ApiOperation("获取历史消息")
    @GetMapping("/getHistoryMsg/{fromId}/{toId}")
    public Result getHistoryMsg(@PathVariable String fromId, @PathVariable String toId) {
        List<MessageVo> historyMsg = messageService.getHistoryMsg(fromId, toId);
        return Result.success(historyMsg);
    }

    @ApiOperation("存储历史消息")
    @PostMapping("/add")
    public Result getHistoryMsg(@RequestBody Message message) {
        return Result.success(messageService.save(message));
    }

    @Resource
    private UserVoService userVoService;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("获取其它用户信息")
    @PostMapping("/getOthers")
    public Result getOthers(HttpServletRequest request) {
        String username = jwtTokenUtil.getUserNameFromRequest(request);
        Wrapper<UserVo> selectUsername = new QueryWrapper<UserVo>().ne("username", username);
        List<UserVo> userVos = userVoService.list(selectUsername);
        List<UserExceptSelfVo> UserExceptSelfVos = userVoService.getUserVos(username);
        System.out.println("userVos = " + userVos);
        return Result.success(UserExceptSelfVos);
    }

    @ApiOperation("通过username查询聊天列表")
    @PostMapping("/getChatByUsername/{inputName}")
    public Result getOthersByUsername(HttpServletRequest request, @PathVariable String inputName) {
        String username = jwtTokenUtil.getUserNameFromRequest(request);
        QueryWrapper<UserVo> selectUserName = new QueryWrapper<UserVo>().ne("username", username);
        //List<UserVo> userVos = userVoService.list(selectUserName);
        if (inputName!=null&&!inputName.equals("")) {
            List<UserExceptSelfVo> userVosByLike = userVoService.getUserVosByLike(username, inputName);
            return Result.success(userVosByLike);
        }else{
            List<UserExceptSelfVo> UserExceptSelfVos = userVoService.getUserVos(username);
            return Result.success(UserExceptSelfVos);
        }



    }

}
